#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Create by yanghongqiang
# Create on 2021/3/24

from util import mysql_util;

def gzbd_mysaql(gzbd_all):
    conn = None;
    cur = None;
    try:
        conn,cur = mysql_util.get_connectin();
        for gzbd in gzbd_all:
            date = gzbd.get("日期");
            query_sql = "select * from spider_coronavirus where date = '%s'"%(date);
            result = mysql_util.execute_query(cur,query_sql);
            if result and len(result) > 0:
                continue;
            insert_sql = "insert into spider_coronavirus (date, region, diagnosis, overseas_import, cure, " \
                         "death, therapy, observation) values ('%s', '%s', %s, %s, %s, %s, %s, %s)" % \
                         (gzbd["日期"], gzbd["地域"], gzbd.get("确诊数", None), gzbd.get("输入数", None),
                          gzbd.get("治愈数", None), gzbd.get("死亡数", None), gzbd.get("隔离数", None),
                          gzbd.get("观察数", None));
            insert_sql = insert_sql.replace("None","Null");
            print(insert_sql);
            mysql_util.execute_query(cur,insert_sql);
            mysql_util.commit_tran(conn);

    except BaseException as e:
        print(e);
        mysql_util.rollback_(conn);
    finally:
        mysql_util.close_conn_cur(conn,cur);
